export function invokeAPi(...args: any[]) {
  return window.xz_call && window.xz_call(...args);
}

export function invokeAsyncAPi(...args:any[]):Promise<any> {
  return new Promise((resolve) => {
    args.push((...args1:any[]) => resolve(args1.length > 1 ? args1 : args1[0]))
    window.xz_call && window.xz_call(...args);
  })
}

export function registerInformApi(apiName:string, cb:any) {
  (window.xz_register && window.xz_register(apiName, cb)) || (window[apiName as any] = cb)
}

export function reportInfo(code:string|number, info:string) {
  invokeAsyncAPi('xz_submit_info_report', code, JSON.stringify(info))
}
/**
 * @param sceneId {Number} 场景id
 * @param reportId {Number} 埋点id
 * @param payload {String|Number} 值
 * @return null
*/
export const reportInfoApi = (sceneId:string, reportId:string, payload:any) => {
  // 参数为字符串类型时
  let methodName = 'information_string_report'
  // 数值类型
  if (/^(-?\d+)(\.\d+)?$/.test(payload)) {
    methodName = 'information_count_report'
  }
  return invokeAsyncAPi(methodName, reportId, sceneId, payload)
}
